#include "setSchoolCal.h"
#include "Settings.h"
#include <QtGui/QMessageBox>

SetSchoolCalDialog::SetSchoolCalDialog()
{
    this->setupUi(this);
    this->setWindowTitle(QString::fromUtf8("设置校历"));
    this->dt_date->setDate(QDate::currentDate());

    connect(this->bt_cancel,SIGNAL(clicked()),this,SLOT(close()));
    connect(this->bt_set,SIGNAL(clicked()),this,SLOT(setSchoolCal()));
}

void SetSchoolCalDialog::setSchoolCal()
{
    int year = this->dt_date->date().year();
    int month = this->dt_date->date().month();
    int day = this->dt_date->date().day();
    QMessageBox::StandardButton rb =QMessageBox::question(this,QString::fromUtf8("确定设置校历"),(QString::fromUtf8("确定将校历设置为")+QString::number(year)+"-"+QString::number(month)+"-"+QString::number(day)+QString::fromUtf8("吗")), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
    if(rb == QMessageBox::Yes)
    {
        QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");//数据库查询
        db.setHostName(HOSTNAME);
        db.setUserName(USERNAME);
        db.setPassword(PASSWORD);
        db.setDatabaseName(DATABASENAME);
        if(!db.open())
        {
            QMessageBox::critical(0,QObject::tr("数据库出错"),db.lastError().text());
            return ;
        }
        QSqlQuery query;
        query.exec("update schoolcal set thatdate=\'"+QString::number(year)+"-"+QString::number(month)+"-"+QString::number(day)+"\' where weekNO=1");

        db.close();
        emit schoolcalchange();
        this->close();
    }
}

SetSchoolCalDialog::~SetSchoolCalDialog()
{

}
